package me.zxk.qwert_poetry.dto.response;

import lombok.Data;
import me.zxk.qwert_poetry.dao.Author;
import me.zxk.qwert_poetry.dao.Poem;
import me.zxk.qwert_poetry.dao.Tag;
import me.zxk.qwert_poetry.dao.User;
import me.zxk.qwert_poetry.service.PoemService;
import me.zxk.qwert_poetry.util.UserContextHolder;

import java.util.List;

@Data
public class PoemDetailResponseDTO {
    private Integer id;
    private String title;
    private String content;
    private Author author;
    private String translation;
    private String annotation;
    private String appreciation;
    private List<Tag> tags;
    private Integer likeCount;
    private Boolean liked;
    private Boolean collected;
    private Integer collectionCount;

    public static PoemDetailResponseDTO fromEntity(Poem poem, PoemService poemService) {
        PoemDetailResponseDTO dto = new PoemDetailResponseDTO();
        dto.setId(poem.getId());
        dto.setTitle(poem.getTitle());
        dto.setContent(poem.getContent());
        dto.setAuthor(poem.getAuthor());
        dto.setTranslation(poem.getTranslation());
        dto.setAnnotation(poem.getAnnotation());
        dto.setAppreciation(poem.getAppreciation());
        dto.setTags(poem.getTags());

        User user = UserContextHolder.getUser();
        if (user != null) {
            dto.setLiked(poemService.isLiked(poem.getId(), user.getId()));
            dto.setCollected(poemService.isCollected(poem.getId(), user.getId()));
        }
        dto.setLikeCount(poemService.getLikeCountById(poem.getId()));
        dto.setCollectionCount(poemService.getCollectCountById(poem.getId()));
        return dto;
    }

    public static Poem toEntity(PoemDetailResponseDTO dto) {
        Poem poem = new Poem();
        poem.setId(dto.getId());
        poem.setTitle(dto.getTitle());
        poem.setContent(dto.getContent());
        poem.setTranslation(dto.getTranslation());
        poem.setAnnotation(dto.getAnnotation());
        poem.setAppreciation(dto.getAppreciation());
        poem.setTags(dto.getTags());
        poem.setAuthor(dto.getAuthor());
        return poem;
    }
}
